package com.yicheng.push;

import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;
import com.fasterxml.jackson.annotation.JsonFormat;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.json.JSONObject;

public class Pusher {

    private static String appId = "wx231f9709f36aa532";
    private static String secret = "22263a27804d09e7f95b59cb8cc7ce7c";



    public static void push(String openId, String templateId) throws Exception{
        String wea = "";//当前天气
        String sunrise = "";//太阳升起时间
        String win = "";//吹的什么风
        String tem = "";//当前温度
        String air_tips = "";//空气质量建议
        //配置模板
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        //推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser(openId).templateId(templateId).build();

        //百度天气
        String path = "http://gwgp-h4bqkmub7dg.n.bdcloudapi.com/day";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.GET, path);
            //百度智能云AppId， 百度智能云 AppSecret
        request.setCredentials("49d1aebca8cc4fdc9d7d597a8582a685", "548296ec26f44a2ba86263971118028a");
        request.addHeaderParameter("Content-Type", "application/json;charset=UTF-8");
        request.addQueryParameter("city", "广安");
        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());
        ApiExplorerResponse response = client.sendRequest(request);
        // 返回结果格式为Json字符串
        JSONObject json = new JSONObject(response.getResult());//将获取到的json字符串转换为json对象
        wea = json.getString("wea");
        sunrise = json.getString("sunrise");
        win = json.getString("");
        tem = json.getString("tem");
        air_tips = json.getString("air_tips");
        //3,如果是正式版发送模版消息，这里需要配置你的信息

        //templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));
        templateMessage.addData(new WxMpTemplateData("wea",wea+"","#FF6347"));
        templateMessage.addData(new WxMpTemplateData("sunrise",sunrise+"","#FF6347"));
        templateMessage.addData(new WxMpTemplateData("win",win+"","#FF6347"));
        templateMessage.addData(new WxMpTemplateData("tem",tem+"","#FF6347"));
        templateMessage.addData(new WxMpTemplateData("air_tips",air_tips+"","#FF6347"));

    }
}
